package rank
{
	import events.TabEvent;
	
	import flash.display.DisplayObject;
	import flash.display.InteractiveObject;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.utils.getDefinitionByName;
	
	import global.GameState;
	
	import utils.DisplayTool;
	//import utils.TextTool;
	//import utils.animate.TweenLight;
	
	//import view.components.tab.Tab;
	//import view.components.tab.TabBar2;
	//import view.components.tab.TabBar2;
	
	public class Rank extends Sprite
	{
		
		
		
		private var prevBut:Object;
		private var prevPageBut:Object;
		private var prevEndBut:Object;
		
		private var nextBut:Object;
		private var nextPageBut:Object;
		private var nextEndBut:Object;
		private var tab:Object;
		
		private var items:Array;
		private var conMask:Sprite;
		private var container:Sprite;
		private var tween:*;
		private var isTweening:Boolean;
		
		private var friendRankText:TextField;
		private var counturyRankText:TextField;
		private var cityRankText:TextField;
		private var schoolRankText:TextField;
		
		private var itemMax:int= 30;
		
		private var friendRankContainer:Sprite;
		private var counturyRankContainer:Sprite;
		private var cityRankContainer:Sprite;
		private var schoolRankContainer:Sprite;
		
		private var ranks:Array = [];
		private var title:InteractiveObject;
		
		private var TabBar2:*;
		private var ImgButton:*;
		private var HintImgButton:*;
		private var Tab:*;
		
		private var TweenLight:*;
		private var XmlLoadMessage:*;
		private var TextTool:*;
		
		private var currentIndex:int;
		
		
		public function Rank()
		{
			
			
		}
		
		public function init():void
		{
			initClass();
			initStage();
			initBg();
			initTitle();
			initBut();
			initTween();
			initItems();
			initText();
			initTab();
			
			
		}
		
		public function load():void
		{
			loadData();
		}
		
		private function initClass():void
		{
			ImgButton = getDefinitionByName("view.components.ImgButton");
			HintImgButton = getDefinitionByName("view.components.HintImgButton");
			Tab = getDefinitionByName("view.components.tab.Tab");
			TabBar2 = getDefinitionByName("view.components.tab.TabBar2");
			TweenLight = getDefinitionByName("utils.animate.TweenLight");
			XmlLoadMessage = getDefinitionByName("model.load.XmlLoadMessage");
			TextTool = getDefinitionByName("utils.TextTool");
		}
		
		private function initTitle():void
		{
			title = new ImgButton(0,-30,-1,onBut,GameState.GAME_WIDTH,30,"butBg5","text_title_rank");
			(title as ImgButton).enabled = false;
			addChild(title);
		}
		
		private function initTab():void
		{
			tab = new Tab(8);
			tab.addTabBar(new TabBar2(0,"全国",50),true);
			tab.addTabBar(new TabBar2(1,"好友",50));
			tab.addTabBar(new TabBar2(2,"本市",50));
			
			if (GameState.sns == "1")
			{
				tab.addTabBar(new TabBar2(3,"本校",50));
			}
			else
			{
				tab.addTabBar(new TabBar2(3,"本站",50));
			}
			tab.y = 5;
			tab.x = 30;
			addChild(tab as DisplayObject);
			
			tab.addEventListener(TabEvent.TABBAR_CLICK,onTab);
			//tab.mouseEnabled = false;
			//tab.mouseChildren = false;
		}
		
		private function onTab(event:TabEvent):void
		{
			if (ranks[0] == null)
			{
				currentIndex = event.index;
				return;
			}
			
			clear();
			update(ranks[event.index],event.index);	
			
			
			switch(event.index)
			{
				case 0:
					counturyRankContainer.visible = true;
					friendRankContainer.visible = false;
					schoolRankContainer.visible = false;
					cityRankContainer.visible = false;
					break;
				case 1:
					counturyRankContainer.visible = false;
					friendRankContainer.visible = true;
					schoolRankContainer.visible = false;
					cityRankContainer.visible = false;
					break;
				case 2:
					counturyRankContainer.visible = false;
					friendRankContainer.visible = false;
					schoolRankContainer.visible = false;
					cityRankContainer.visible = true;
					break;
				case 3:
					counturyRankContainer.visible = false;
					friendRankContainer.visible = false;
					schoolRankContainer.visible = true;
					cityRankContainer.visible = false;
					break;
			}
		}
		
		private function initText():void
		{
			friendRankContainer = new Sprite();
			friendRankContainer.mouseChildren = false;
			friendRankContainer.mouseEnabled = false;
			counturyRankContainer = new Sprite();
			counturyRankContainer.mouseChildren = false;
			counturyRankContainer.mouseEnabled = false;
			cityRankContainer = new Sprite();
			cityRankContainer.mouseChildren = false;
			cityRankContainer.mouseEnabled = false;
			schoolRankContainer = new Sprite();
			schoolRankContainer.mouseChildren = false;
			schoolRankContainer.mouseEnabled = false;
			
			addChild(TextTool.getTextFieldXY(500,5,100,20,12,0xFFFFFF,"本人排名",false,true));
			friendRankContainer.addChild(TextTool.getTextFieldXY(0,0,40,20,12,0xDDDDDD,"好友:",false,true));		
			counturyRankContainer.addChild(TextTool.getTextFieldXY(0,0,40,20,12,0xDDDDDD,"全国:",false,true));	
			cityRankContainer.addChild(TextTool.getTextFieldXY(0,0,40,20,12,0xDDDDDD,"本市:",false,true));
			
			if (GameState.sns == "1")
			{
				schoolRankContainer.addChild(TextTool.getTextFieldXY(0,0,40,20,12,0xDDDDDD,"本校:",false,true));
			}
			else
			{
				schoolRankContainer.addChild(TextTool.getTextFieldXY(0,0,40,20,12,0xDDDDDD,"本站:",false,true));
			}
			friendRankText = TextTool.getTextFieldXY(40,0,30,20,12,0xFFFFFF,"");
			counturyRankText = TextTool.getTextFieldXY(40,0,30,20,12,0xFFFFFF,"");
			cityRankText = TextTool.getTextFieldXY(40,0,30,20,12,0xFFFFFF,"");
			schoolRankText = TextTool.getTextFieldXY(40,0,30,20,12,0xFFFFFF,"");
			
			schoolRankContainer.addChild(schoolRankText);
			cityRankContainer.addChild(cityRankText);
			friendRankContainer.addChild(friendRankText);
			counturyRankContainer.addChild(counturyRankText);
			
			
			
			friendRankContainer.x = counturyRankContainer.x = schoolRankContainer.x = cityRankContainer.x =570;
			friendRankContainer.y = counturyRankContainer.y = schoolRankContainer.y = cityRankContainer.y = 5;
			addChild(schoolRankContainer);
			addChild(cityRankContainer);
			addChild(friendRankContainer);
			addChild(counturyRankContainer);
			
			counturyRankContainer.visible = true;
			friendRankContainer.visible = false;
			schoolRankContainer.visible = false;
			cityRankContainer.visible = false;
		}
		
		private function initStage():void
		{
			if (stage)
			{
				stage.scaleMode = "noScale"
			}
		}
		
		private function initBg():void
		{
			with(this.graphics)
			{
				
				
				beginFill(0x444444);
				drawRect(0,0,GameState.GAME_WIDTH,400);
				endFill();
				
				lineStyle(3,0xCCCCCC);
				drawRoundRect(30,30,710,145,15,15);
			}
		}
		
		private function initBut():void
		{
			items = [];
			container = new Sprite();
			conMask = new Sprite();
			addChild(conMask);
			conMask.addChild(container);
			
			conMask.x = 58;
			conMask.y = 30;
			conMask.scrollRect = new Rectangle(0,0,670,150);
			
			
			prevBut = new HintImgButton(20,65,0,onBut,new RankTool.Prev(),-1,-1,null);
			prevPageBut = new HintImgButton(20,95,1,onBut,new RankTool.PrevPage(),-1,-1,null);
			prevEndBut = new HintImgButton(20,125,2,onBut,new RankTool.PrevEnd(),-1,-1,null);
			
			addChild(prevBut as DisplayObject);
			addChild(prevPageBut as DisplayObject);
			addChild(prevEndBut as DisplayObject);
			
			nextBut = new HintImgButton(730,65,3,onBut,new RankTool.Next(),-1,-1,null);
			nextPageBut = new HintImgButton(730,95,4,onBut,new RankTool.NextPage(),-1,-1,null);
			nextEndBut = new HintImgButton(730,125,5,onBut,new RankTool.NextEnd(),-1,-1,null);
			
			addChild(nextBut as DisplayObject);
			addChild(nextPageBut as DisplayObject);
			addChild(nextEndBut as DisplayObject);
			
			prevBut.info = "上一个";
			prevPageBut.info = "上一页";
			prevEndBut.info = "第一页";
			
			nextBut.info = "下一个";
			nextEndBut.info = "最后一页";
			nextPageBut.info = "下一页";
		}
		
		private function onBut(event:MouseEvent):void
		{
			if (tween.isTweening)
			{
				return;
			}
			
			switch (event.target.id)
			{
				case 0:
					if (container.x < 0)
					{
						tween.setTarget(container,container.x, 0, container.x + 97,0);
						tween.start();
					}
					break;
				case 1:
					if (container.x + 97 * 7  < 0)
					{
						tween.setTarget(container,container.x, 0, container.x + 97 * 7,0);
						tween.start();
					}
					else if (container.x < 0)
					{
						tween.setTarget(container,container.x, 0, 0,0);
						tween.start();
					}
					break;
				case 2:
					tween.setTarget(container,container.x, 0, 0,0);
					tween.start();
					break;
				case 3:
					if ( Math.abs(container.x - 670) < container.width)
					{
						tween.setTarget(container,container.x, 0, container.x - 97,0);
						tween.start();
					}
					break;
				case 4:
					if ( Math.abs(container.x - 670 - 97 * 7) < container.width)
					{
						tween.setTarget(container,container.x, 0, container.x - 97 * 7,0);
						tween.start();
					}
					else if (Math.abs(container.x - 670) < container.width)
					{
						tween.setTarget(container,container.x, 0, - (container.width - 670),0);
						tween.start();
					}
					break;
				case 5:
					
					if (container.width > 670)
					{
						tween.setTarget(container,container.x, 0, - (container.width - 670),0);
						tween.start();
					}
					break;
				
			}
			
			
			
		}
		
		private function initTween():void
		{
			
			tween = new TweenLight();
			tween.duration = .3;
			//tween.addEventListener(Event.COMPLETE,onTweenComplete);
		}
		
		
		
		
		
		private function loadData():void
		{
			var msg:* = new XmlLoadMessage(updateAll);
			msg.url = GameState.getServerLink() + "44&key=" + GameState.snsKey;
			//msg.id = type;
			msg.send();
		}
		
		private function initItems():void
		{
			var item:RankItem;
			for (var i:int=0; i<itemMax; i++)
			{
				item = new RankItem();
				item.index = i + 1;
				item.x = i * 97 + 38;
				item.y = 78;
				items[i] = item;
			}
		}
		
		private function updateAll(xml:XML,type:int):void
		{
			var index:int;
			var ele:XML;
			for each(ele in xml.children())
			{
				index = int(ele.@type);
				ranks[index] = ele;
				if (index == currentIndex)
				{
					update(ele,index);
				}
			}
			//tab.mouseEnabled = true;
			//tab.mouseChildren = true;
		}
		
		private function update(xml:XML,id:int):void
		{
			
			ranks[id] = xml;
			
			var data:XML;
			var item:RankItem;
			var index:int;
			for each (data in xml.children())
			{
				
				item = items[index];
				if (item)
				{
					item.update(data);
					container.addChild(item);
				}
				index++;
			}
			
			switch(id)
			{
				case 0:
					counturyRankText.text = xml.@index;
					break;
				case 1:
					friendRankText.text = xml.@index;
					break;
				case 2:
					cityRankText.text = xml.@index;
					break;
				case 3:
					schoolRankText.text = xml.@index;
					break;
			}
		}
		
		private function clear():void
		{
			var item:RankItem;
			for each (item in items)
			{
				item.clear();
			}
			DisplayTool.removeAllChild(container);
			container.x = 0;
		}
	}
}